"
" vim-go config 
"
"

let g:go_doc_popup_window = 1
let g:go_fmt_command = "goimports"
let g:go_fmt_fail_silently = 1
let g:go_debug_windows = {
            \ 'vars':  'leftabove 35vnew',
            \ 'stack': 'botright 10new',
            \ }
let g:go_test_show_name = 1
let g:go_list_type = "quickfix"
let g:go_autodetect_gopath = 1
let g:go_metalinter_autosave_enabled = ['vet', 'golint']
let g:go_metalinter_enabled = ['vet', 'golint']
let g:go_gopls_complete_unimported = 1
let g:go_diagnostics_level = 2

" nmap <c-g> :GoDecls<cr>
" imap <c-g> <esc>:<c-u>GoDecls<cr>

augroup go
    autocmd!
    autocmd FileType go nmap <silent> <Leader>v <Plug>(go-def-vertical)
    autocmd FileType go nmap <silent> <Leader>s <Plug>(go-def-split)
    autocmd FileType go nmap <silent> <Leader>d <Plug>(go-def-tab)
    autocmd FileType go nmap <silent> <Leader>x <Plug>(go-doc-vertical)
    autocmd FileType go nmap <silent> <Leader>i <Plug>(go-info)
    autocmd FileType go nmap <silent> <Leader>l <Plug>(go-metalinter)
    autocmd FileType go nmap <silent> <leader>b :<C-u>call <SID>build_go_files()<CR>
    autocmd FileType go nmap <silent> <leader>t  <Plug>(go-test)
    autocmd FileType go nmap <silent> <leader>r  <Plug>(go-run)
    autocmd FileType go nmap <silent> <leader>e  <Plug>(go-install)
    autocmd FileType go nmap <silent> <Leader>c <Plug>(go-coverage-toggle)

    " I like these more!
    autocmd Filetype go command! -bang A call go#alternate#Switch(<bang>0, 'edit')
    autocmd Filetype go command! -bang AV call go#alternate#Switch(<bang>0, 'vsplit')
    autocmd Filetype go command! -bang AS call go#alternate#Switch(<bang>0, 'split')
    autocmd Filetype go command! -bang AT call go#alternate#Switch(<bang>0, 'tabe')
augroup END

" run :GoBuild or :GoTestCompile based on the go file
function! s:build_go_files()
    let l:file = expand('%')
    if l:file =~# '^\f\+_test\.go$'
        call go#test#Test(0, 1)
    elseif l:file =~# '^\f\+\.go$'
        call go#cmd#Build(0)
    endif
endfunction
